package com.su.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.su.common.R;
import com.su.pojo.Employee;
import com.su.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * 员工
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     *
     * @param req
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest req, @RequestBody Employee employee) {
        //md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        password = password + "emp";
        String pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        //password = DigestUtils.md5DigestAsHex(password.getBytes());
        //查询条件
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        lqw.eq(Employee::getPassword, pwd);
        Employee emp = employeeService.getOne(lqw);
        //用户不存在
        if (emp == null) {
            return R.error("用户名或密码错误");
        }
        //禁用状态
        if (emp.getStatus() == 0) {
            return R.error("账号不可用");
        }
        req.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 员工退出账号
     *
     * @param req
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest req) {
        //清除session中的数据
        req.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 添加员工
     *
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> add(HttpServletRequest req, @RequestBody Employee employee) {
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        password = password + "emp";
        String pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        employee.setPassword(pwd);
        long id = Thread.currentThread().getId();
        //log.info("当前线程: {}",id);
        //(自动填充)
       /* employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Long empId = (Long) req.getSession().getAttribute("employee");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);*/
        employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 分页展示员工信息
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> getByPage(int page, int pageSize, String name) {
        //log.info("page{},pageSize{},name{}", page, pageSize, name);
        Page page1 = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper();
        //添加过滤条件
        lqw.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        lqw.orderByDesc(Employee::getUpdateTime);
        employeeService.page(page1, lqw);
        return R.success(page1);
    }

    /**
     * 根据id修改员工信息
     *
     * @param employee
     * @return
     */
    @PutMapping()
    public R<String> updateById(HttpServletRequest req, @RequestBody Employee employee) {
        //log.info(employee.toString());
        long id = Thread.currentThread().getId();
        //log.info("当前线程: {}",id);
        //获取操作者的信息(自动填充)
        /*Long empId = (Long) req.getSession().getAttribute("employee");
        //设置更新的信息
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);*/
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> selectById(@PathVariable Long id) {
        //log.info("id {}", id);
        Employee emp = employeeService.getById(id);
        if (emp != null) {
            return R.success(emp);
        }
        return R.error("没有该员工信息");
    }
}
